# 成功生成图片的代码 - 执行时间: 2025-11-03 12:18:00
# ==================================================
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from matplotlib.patches import Rectangle, FancyBboxPatch
import numpy as np
import pandas as pd

# 设置中文字体
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
plt.rcParams['axes.unicode_minus'] = False

# 创建数据集结构图
fig, ax = plt.subplots(figsize=(12, 8))
fig.patch.set_facecolor('#f5f5f5')

# 标题
ax.text(0.5, 0.95, '西瓜籽粒400数据集结构', ha='center', va='top', 
        fontsize=18, fontweight='bold', color='#333')

# 数据集信息框
dataset_box = FancyBboxPatch((0.1, 0.75), 0.8, 0.15,
                           boxstyle="round,pad=0.02",
                           facecolor='#e6f7ff', edgecolor='#1890ff', linewidth=2)
ax.add_patch(dataset_box)
ax.text(0.5, 0.825, '西瓜籽粒400数据集', ha='center', va='center', 
        fontsize=14, fontweight='bold', color='#1890ff')
ax.text(0.5, 0.79, '1040张专业标注图像', ha='center', va='center', 
        fontsize=12, color='#333')
ax.text(0.5, 0.76, 'YOLOv8格式标注 | 单一类别: grain', ha='center', va='center', 
        fontsize=12, color='#333')

# 数据增强方法
enhancement_box = FancyBboxPatch((0.1, 0.55), 0.8, 0.15,
                               boxstyle="round,pad=0.02",
                               facecolor='#f6ffed', edgecolor='#52c41a', linewidth=2)
ax.add_patch(enhancement_box)
ax.text(0.5, 0.625, '数据增强方法', ha='center', va='center', 
        fontsize=14, fontweight='bold', color='#52c41a')

# 增强方法列表
enhancements = [
    "水平翻转 (50%概率)",
    "垂直翻转 (50%概率)",
    "90度旋转 (四种方向)",
    "随机裁剪 (0-15%区域)",
    "随机旋转 (-45°至+45°)",
    "随机剪切 (水平±25°, 垂直±23°)",
    "椒盐噪声 (5%像素)"
]

for i, method in enumerate(enhancements):
    ax.text(0.15, 0.58 - i*0.015, f"• {method}", va='center', fontsize=10, color='#333')

# 数据集划分
split_box = FancyBboxPatch((0.1, 0.25), 0.8, 0.25,
                         boxstyle="round,pad=0.02",
                         facecolor='#fff2e8', edgecolor='#fa8c16', linewidth=2)
ax.add_patch(split_box)
ax.text(0.5, 0.40, '数据集划分', ha='center', va='center', 
        fontsize=14, fontweight='bold', color='#fa8c16')

# 绘制数据集划分饼图
split_sizes = [70, 15, 15]  # 训练集70%，验证集15%，测试集15%
split_labels = ['训练集', '验证集', '测试集']
colors = ['#1890ff', '#52c41a', '#fa8c16']

wedges, texts, autotexts = ax.pie(split_sizes, 
                                  labels=split_labels, 
                                  colors=colors,
                                  autopct='%1.1f%%',
                                  startangle=90,
                                  textprops={'fontsize': 12, 'color': '#333'},
                                  pctdistance=0.85)

# 添加中心圆
centre_circle = plt.Circle((0.5, 0.325), 0.15, color='white')
ax.add_artist(centre_circle)
ax.text(0.5, 0.325, '1040张\n图像', ha='center', va='center', 
        fontsize=12, fontweight='bold', color='#333')

# 元数据信息
metadata_text = (
    "创建日期: 2023年9月13日\n"
    "导出日期: 2025年6月24日\n"
    "许可证: CC BY 4.0\n"
    "来源: qunshankj平台"
)
ax.text(0.5, 0.08, metadata_text, ha='center', va='center', 
        fontsize=10, color='#666', style='italic')

# 移除坐标轴
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.axis('off')

# 调整布局
plt.tight_layout()

# 保存图片
plt.savefig('pre_upload_dir/西瓜籽粒400数据集结构图.png', dpi=300, bbox_inches='tight')
plt.close()

# 创建数据增强效果示意图
fig, axes = plt.subplots(2, 4, figsize=(16, 8))
fig.patch.set_facecolor('#f5f5f5')

# 增强方法标题
plt.suptitle('数据增强方法示意图', fontsize=16, fontweight='bold', y=0.98)

# 增强方法名称
methods = ['原始图像', '水平翻转', '垂直翻转', '90度旋转', 
          '随机裁剪', '随机旋转', '随机剪切', '椒盐噪声']

# 模拟增强效果（使用随机矩形模拟）
np.random.seed(42)
for i, ax in enumerate(axes.flat):
    # 绘制背景
    ax.add_patch(Rectangle((0, 0), 1, 1, facecolor='#f0f0f0'))
    
    # 绘制模拟的西瓜籽
    if methods[i] == '原始图像':
        # 原始图像：均匀分布
        positions = [(0.2, 0.3), (0.5, 0.4), (0.7, 0.2), (0.3, 0.7), (0.6, 0.6)]
    elif methods[i] == '水平翻转':
        # 水平翻转：x坐标反转
        positions = [(0.8, 0.3), (0.5, 0.4), (0.3, 0.2), (0.7, 0.7), (0.4, 0.6)]
    elif methods[i] == '垂直翻转':
        # 垂直翻转：y坐标反转
        positions = [(0.2, 0.7), (0.5, 0.6), (0.7, 0.8), (0.3, 0.3), (0.6, 0.4)]
    elif methods[i] == '90度旋转':
        # 90度旋转：坐标变换
        positions = [(0.7, 0.8), (0.6, 0.5), (0.8, 0.3), (0.3, 0.3), (0.4, 0.4)]
    elif methods[i] == '随机裁剪':
        # 随机裁剪：部分区域缺失
        positions = [(0.4, 0.5), (0.6, 0.6)]
    elif methods[i] == '随机旋转':
        # 随机旋转：位置偏移
        positions = [(0.25, 0.35), (0.55, 0.45), (0.65, 0.25), (0.35, 0.65), (0.65, 0.65)]
    elif methods[i] == '随机剪切':
        # 随机剪切：变形效果
        positions = [(0.15, 0.35), (0.45, 0.45), (0.75, 0.15), (0.25, 0.75), (0.55, 0.55)]
    else:  # 椒盐噪声
        # 椒盐噪声：随机点
        positions = [(0.2, 0.3), (0.5, 0.4), (0.7, 0.2), (0.3, 0.7), (0.6, 0.6)]
        # 添加噪声点
        for _ in range(20):
            noise_x = np.random.uniform(0, 1)
            noise_y = np.random.uniform(0, 1)
            ax.plot(noise_x, noise_y, 'o', color='black', markersize=1)
    
    # 绘制西瓜籽
    for x, y in positions:
        ax.add_patch(plt.Circle((x, y), 0.05, color='#ff6b6b', alpha=0.8))
    
    # 设置标题
    ax.set_title(methods[i], fontsize=12)
    ax.set_xlim(0, 1)
    ax.set_ylim(0, 1)
    ax.set_aspect('equal')
    ax.axis('off')

# 调整布局
plt.tight_layout(rect=[0, 0, 1, 0.95])

# 保存图片
plt.savefig('pre_upload_dir/西瓜籽粒数据增强方法示意图.png', dpi=300, bbox_inches='tight')
plt.close()
